package com.bw.zg2.view.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.zg2.R
import com.bw.zg2.base.BaseActivity
import com.bw.zg2.databinding.ActivityRegisterBinding
import com.bw.zg2.viewmodel.RegisterViewModel

/**
 *
 * @ClassName: RegisterActivity
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/12 12:05 PM
 * @Version: 1.0
 */
@Route(path = "/login/RegisterActivity")
class RegisterActivity :
    BaseActivity<RegisterViewModel, ActivityRegisterBinding>(R.layout.activity_register) {
    override fun initData() {
        viewModel.registerLiveData.observe(this) {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
                SPUtils.getInstance().put("name",it.data.username)
                SPUtils.getInstance().put("pass",it.data.password)
                startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))
                finish()
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        dataBinding.btn.setOnClickListener {

            //去注册
            val name = dataBinding.etName.text.toString()
            val pass = dataBinding.etPass.text.toString()
            val rePass = dataBinding.etRepass.text.toString()

            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass) || TextUtils.isEmpty(rePass)) {
                ToastUtils.showShort("用户名或密码不能为空")
                return@setOnClickListener
            }

            if (pass != rePass) {
                ToastUtils.showShort("两次输入密码不一致")
                return@setOnClickListener
            }

            val map = HashMap<String, String>()
            map["username"] = name
            map["password"] = pass
            map["repassword"] = rePass
            viewModel.register(map)

        }
    }

}